
//  LCVideoDetailsListsViewController.m
//  JKFuYinProject
//
//  Created by admin on 2018/4/20.
//  Copyright © 2018年 LC. All rights reserved.
//



#import "LCVideoDetailsListsViewController.h"
#import "NetworkTool+LCAVPlay.h"

@interface LCVideoDetailsListsViewController ()
<UITableViewDelegate, UITableViewDataSource>

@property(strong, nonatomic) UITableView *tableView;

@end

@implementation LCVideoDetailsListsViewController

- (void)loadView {
    [super loadView];
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [LCFloatingWindow sharedInstance].hidden = YES;
}
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    
}
- (void)dealloc{
    NSLog(@"__func__dealloc = %s", __func__);
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"节目";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tabBarController.tabBar.translucent = NO;
    self.view.backgroundColor = arc4random_color;//调用随机色

    [self.tableView registerClass:[LSZBDetailsPlayTableViewCell class] forCellReuseIdentifier:@"LSZBDetailsPlayTableViewCell"];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 20)];
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11, *)) {
            make.left.right.equalTo(self.view);
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
        }else{
            make.edges.equalTo(self.view);
        }
    }];
    [self hideTheKeyboard];
}
//隐藏键盘Hide the keyboard
-(void)hideTheKeyboard{
    self.tableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
}

#pragma mark - loadData
-(void)setMp4DetailModel:(LCMp4DetailModel *)mp4DetailModel{
    _mp4DetailModel = mp4DetailModel;
    if (mp4DetailModel) {
        self.arrayData = mp4DetailModel.list;
        [self.tableView reloadData];
    }
}

#pragma mark - UITableView data source -
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.arrayData.count;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (ScreenWidth == 320) {
        //5
        return 50;
    }else if(ScreenWidth == 375){
        // 6 7
        return 55;
    }else {
        //plus
        return 60;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LSZBDetailsPlayTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LSZBDetailsPlayTableViewCell"];
    [self contentWithCellForRow:indexPath :cell];
    return cell;
}
#pragma mark - 在线内容的cellForRow
-(void)contentWithCellForRow :(NSIndexPath *)indexPath :(LSZBDetailsPlayTableViewCell *)cell
{
    cell.labelNumber.text = [NSString stringWithFormat:@"%ld",(long)indexPath.row+1];
    cell.labelTitle.text = NSStr(self.arrayData[indexPath.row].name);
    cell.labelPlayNumber.text = [NSString stringWithFormat:@"%@次",self.arrayData[indexPath.row].click];
//    NSString * strTitle = [NSString stringWithFormat:@"   %ld   %@",(long)indexPath.row+1,self.arrayData[indexPath.row].name];
//    cell.labelTitle.text = strTitle;
//    [LSZSpecialPackaging adaptiveWidth:cell.labelTitle str:cell.labelTitle.text sizeMaxWidth:ScreenWidth-60 sizeMaxHeight:cell.labelTitle.frame.size.height];
//    CGRect rect = cell.imageViewPlay.frame;
//    rect.origin.x = CGRectGetMaxX(cell.labelTitle.frame)+10;
//    cell.imageViewPlay.frame = rect;
    //次数
//    cell.labelPlayNumber.text = [NSString stringWithFormat:@"%@",self.arrayData[indexPath.row].click];
//    [LSZSpecialPackaging adaptiveWidth:cell.labelPlayNumber str:cell.labelPlayNumber.text sizeMaxWidth:ScreenWidth-50-CGRectGetMaxX(cell.imageViewPlay.frame) sizeMaxHeight:cell.labelPlayNumber.frame.size.height];
//    CGRect rectPlays = cell.labelPlayNumber.frame;
//    rectPlays.origin.x = CGRectGetMaxX(cell.imageViewPlay.frame)+2;
//    cell.labelPlayNumber.frame = rectPlays;

//    if (CGRectGetMaxX(cell.labelPlayNumber.frame) > ScreenWidth-60) {
//        cell.imageViewPlay.hidden = YES;
//        cell.labelPlayNumber.hidden = YES;
//    }else{
//        cell.imageViewPlay.hidden = NO;
//        cell.labelPlayNumber.hidden = NO;
//    }
    cell.butDownload.tag = indexPath.row+1;
    __weak typeof(self) weakSelf = self;
    [cell.butDownload tapGesture:^(UIGestureRecognizer *ges) {
        [weakSelf clickDownload:indexPath.row];//下载
    }];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [self playInterfaceJump:indexPath.row];
}
#pragma mark- 这个是先竖屏播放 和上面那个二选一
-(void)playInterfaceJump :(NSInteger)beforeRow
{
//    LSZIjkplayerPlayerMg *mg = nil;
//    mg = self.ijkplayerPlayerManager.arrayData[beforeRow];
    Mp4List *mg = self.arrayData[beforeRow];
    LSZVideoPlaybackViewController * VC = [[LSZVideoPlaybackViewController alloc]init];
    VC.id_id = self.id_id;
    VC.playUrl = mg.mp4_url;
    VC.click = mg.click;
    VC.titleString = self.mp4DetailModel.detail.name;
    VC.author = self.mp4DetailModel.detail.author;
    VC.isOffline = NO;
    VC.pic = self.mp4DetailModel.detail.pic;
    VC.zan = [self.mp4DetailModel.detail.zan integerValue];
//    VC.numberRow = beforeRow;
    [self.navigationController pushViewController:VC animated:YES];
    [LSZRequestData UpdateVideoPlaybackTimes:self.id_id:^(BOOL isOk, NSString * str, NSDictionary * dic) {
        if (isOk == YES) {
            mg.click = [NSString stringWithFormat:@"%d",[mg.click intValue] +1];
            [self.tableView reloadData];
        }
     }];
}
- (UITableView *)tableView {
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.bounces = NO;
    }
    return _tableView;
}

- (void)clickDownload :(NSInteger)beforeRow {
    Mp4List *mp4List = nil;
    mp4List = self.arrayData[beforeRow];
    [kNetworkTool lcDownloadMp4WithMg:mp4List :self.mp4DetailModel];
}

@end
